package com.example.demoweb.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.demoweb.entity.Student;
import com.example.demoweb.service.StudentService;

/**
 * Created by ChristChild on 2017/9/23.
 */
@RestController
@RequestMapping("student")
public class StudentController {
	@Autowired
	private StudentService studentService;
    /**
     * 根据ID获取student，接收ID参数
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    public Student getStudentById(@PathVariable("id") Integer id){
        return studentService.getStudentById(id);

    }

    @RequestMapping("/index/{id}")
    public ModelAndView index(@PathVariable("id") Integer id) {
        Student student = this.getStudentById(id);
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("student",student);
        return mv;
    }
    
    @PostMapping("/test/abc")
    public void studentTest(HttpServletRequest request) {
    	HttpServletRequest httpRequest=(HttpServletRequest) request;
		Map<String,String[]> dataMap = request.getParameterMap();
		String[] dataArray = dataMap.get("streamData");
		String streamData=httpRequest.getHeader("streamData");
        System.out.println(request);
    }
    
    @GetMapping("/searchList")
    public List<Student> searchList(){
    	List<Student> list = studentService.getList();
    	return list;
    }
    
    /**
     * mysql 版本的批量插入
     * 一次性操作,如果有一条插入失败则全部回滚
     */
    @GetMapping("/insertBatch")
    public void insertBatch(){
    	List<Student> list = new ArrayList<Student>();
    	Student student1 = new Student();
		student1.setName("黄勇");
		student1.setAge(33);
		list.add(student1);
		
		Student student2 = new Student();
		student2.setName("柯尔克");
		student2.setAge(23);
		list.add(student2);
		
		Student student3 = new Student();
		student3.setName("卡卡");
		student3.setAge(35);
		list.add(student3);
		studentService.insertBatch(list);
    }

}
